// app.js
App({
  onLaunch() {
    this.login()
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
  },
  globalData: {
    userInfo: null
  },
  //微信小程序登录
  login() {
    let _this = this
    wx.login({
      success(res) {
        var code = res.code
        wx.request({
          url: 'https://api.it120.cc/zengjunwei/user/wxapp/login',
          method: "POST",
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          data: {
            code: code
          },
          success(res) {
            if(res.data.code == 0){
              wx.setStorageSync('token', res.data.data.token)
            }
            if (res.data.code == 10000) {
              _this.mobie()
            }
          }
        })
      }
    })
  },
  //注册接口
  mobie() {
    var _this=this
    wx.getUserInfo({
      success(res) {
        let encryptedData = res.encryptedData
        let iv = res.iv
        wx.login({
          success(res) {
            let code = res.code
            wx.request({
              url: 'https://api.it120.cc/zengjunwei/user/wxapp/register/complex',
              method: "POST",
              header: {
                "Content-Type": "application/x-www-form-urlencoded"
              },
              data: {
                encryptedData: encryptedData,
                code: code,
                iv: iv
              },
              success(res) {
                if(res.data.code==0){
                  _this.login()
                }
              }
            })
          }
        })
      }
    })

  }

})